<?php
namespace app;

#[\Attribute]
class Role
{
    public $name = null;
    public $auth = [];
    public function __construct($name,array $auth)
    {
        $this->name = $name;
        $this->auth = $auth;
    }
}

class RoleController
{
    #[Role('管理',['read'])]
    public function getlist()
    {}
}

$reflection = new \ReflectionClass(RoleController::class);
/**
 * @var \ReflectionMethod[] $reflectionMethods
 */
$reflectionMethods = $reflection->getMethods();

foreach ($reflectionMethods as $reflectionMethod) {
    $attrs = $reflectionMethod->getAttributes('app\Role');
// array(1) {
//   [0]=>
//   object(ReflectionAttribute)#3 (1) {
//     ["name"]=>
//     string(8) "app\Role"
//   }
// }
    var_dump($attrs);
}

$route = [
    'getlist'=>[
        'clazz'=>RoleController::class,
        'method'=>'getlist',
    ]
];
$url = 'getlist';

$reflection =  new \ReflectionClass($route[$url]['clazz']);
$reflectionMethods = $reflection->getMethods();
$methods = [];
foreach ($reflectionMethods as $method){
    $methods[$method->getName()] = $method;
}

$attrs = $methods[$route[$url]['method']]->getAttributes('app\Role');
// array(2) {
//   [0]=>
//   string(6) "管理"
//   [1]=>
//   array(1) {
//     [0]=>
//     string(4) "read"
//   }
// }
var_dump($attrs[0]->getArguments());

$role = $attrs[0]->newInstance();
// array(1) {        
//   [0]=>
//   string(4) "read"
// }
var_dump($role->auth);

if(in_array('read',$role->auth)){
    echo '可以通行';
}else{
    echo '没有权限';
}





